/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/

public final class MatrixViewTableDialog extends TabbedDialog {
	
	private static final long serialVersionUID = 1L;
	private String title;
	private int nLines;
	private int nTables =0;
	private ArrayList<MatrixViewTable> tables = new ArrayList<MatrixViewTable>();

	/**
	 * Constructor method.
	 *
	 * @param title   a dialog title.
	 * @see javax.swing
	 */
	public MatrixViewTableDialog(String title, int nLines) {
		if(title == null || nLines <= 0){
		this.showErrorMessage("Erron in InpOutTableDialog constructor"
					+"\nTable Title = "+title
					+"\nTable Numero de Linhas = "+nLines);
		System.exit(0);
		}
		this.title = title;
		this.nLines = nLines;
		this.nTables = 0;
	}//End of OutputTableDialog

	
	public void addOutputTable(String name, String[] colNames, JButton[][] cells) {
		if(name == null || colNames == null || cells ==null){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
		+"\nCells = "+cells);

		System.exit(0);
		}	

		if(cells.length != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		
		this.tables.add(nTables, new MatrixViewTable(name, colNames, cells));
		nTables++;
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		
		JPanel linePaneMae = new JPanel();
		linePaneMae.setLayout( new GridBagLayout());
		
		JPanel linePane = new JPanel();
		linePane.setLayout(new GridLayout(1,colNames.length,0,0));
		
		
		
		
		for(int i=0; i < colNames.length; i++)
		{
			JButton button = new JButton(colNames[i]);
			Dimension d = new Dimension();
			d.width=110;
			d.height=25;
			button.setPreferredSize(d);
			button.setMaximumSize(d);
		    linePane.add(button);
		}
		
		linePaneMae.add(linePane);
		mainPane.add(linePaneMae);
		
		JPanel tablePaneMae = new JPanel();
		tablePaneMae.setLayout( new GridBagLayout());

		JPanel tablePane = new JPanel();
		tablePane.setLayout(new GridLayout(nLines,colNames.length,0,0));
		
		
		
		
		for(int i=0; i < nLines; i++)
		for(int j=0; j < colNames.length; j++)
		{
			Dimension d = new Dimension();
			d.width=110;
			d.height=25;
			cells[i][j].setPreferredSize(d);
			cells[i][j].setMaximumSize(d);
			tablePane.add(cells[i][j]);
		}
		tablePaneMae.add(tablePane);
		mainPane.add(tablePaneMae);
		addTab(name, mainPane);
	} //End of addOutputTable
    
	
	public void addOutputTable(String name, String[] lineNames, String[] colNames, JButton[][] cells) {
		if(name == null || colNames == null || lineNames == null || cells ==null){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
					+"\nLine Names = "+lineNames
		+"\nCells = "+cells);

		System.exit(0);
		}	

		if(cells.length != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		
		this.tables.add(nTables, new MatrixViewTable(name, lineNames, colNames, cells));
		nTables++;
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		
		JPanel linePane = new JPanel();
		linePane.setLayout(new GridLayout(1,colNames.length,0,0));
		for(int i=0; i < colNames.length; i++)
		{
			JButton button = new JButton(colNames[i]);
		    linePane.add(button);
		}
		mainPane.add(linePane);
		

		JPanel tablePane = new JPanel();
		tablePane.setLayout(new GridLayout(nLines,colNames.length,0,0));
		for(int i=0; i < nLines; i++)
		for(int j=0; j < colNames.length; j++)
		{
			tablePane.add(cells[i][j]);
		}
		mainPane.add(tablePane);
		
		JPanel colPane = new JPanel();
		colPane.setLayout(new GridLayout(lineNames.length+1,1,0,0));
		JButton vazio = new JButton(" ");
		colPane.add(vazio);
		for(int i=0; i < lineNames.length; i++)
		{
			JButton button = new JButton(lineNames[i]);
		    colPane.add(button);
		}
		JPanel totalPanel = new JPanel();
		totalPanel.setLayout(new BorderLayout());
		totalPanel.add(colPane,"West");
		totalPanel.add(mainPane,"Center");
		addTab(name, totalPanel);
	} //End of addOutputTable
	
	public void addOutputTable(String name, String[] colNames, ArrayList cells) {
		if(name == null || colNames == null || cells ==null){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nTable Name = "+name
					+"\nColumn Names = "+colNames
		+"\nCells = "+cells);

		System.exit(0);
		}	

		if(cells.size() != nLines){
		this.showErrorMessage("Erron in addOutputTable method"
					+"\nThe Number of Lines have to be "+nLines);
		System.exit(0);
		}
		
		this.tables.add(nTables, new MatrixViewTable(name, colNames, cells));
		nTables++;
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		JPanel linePane = new JPanel();
		linePane.setLayout(new GridLayout(1,colNames.length,0,0));
		for(int i=0; i < colNames.length; i++)
		{
			JButton button = new JButton(colNames[i]);
		    linePane.add(button);
		}
		mainPane.add(linePane);

		JPanel tablePane = new JPanel();
		tablePane.setLayout(new GridLayout(nLines,colNames.length,0,0));
		//for(int i=0; i < nLines; i++)
		ListIterator b = cells.listIterator();
		while(b.hasNext()){
		      JButton but = (JButton)b.next();
			  tablePane.add(but);
		}
		
		mainPane.add(tablePane);
		addTab(name, mainPane);
	} //End of addOutputTable
	
	/**
	 * Method to show the dialog.
	 *
	 * @return  an integer indicating the user option: 1 = Cancel, 0 = OK.
	 * @see javax.swing
	 */
	public int showDialog() {
		return super.showDialog(new JFrame(), title);
	}

	/**
	 * Method to be executed for the OK option.
	 *
	 */
	public void approvedPerform() {
		try {
			this.setVisible(false);
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}

	private void showErrorMessage(String msg){
		JOptionPane.showMessageDialog(null,msg,"Error!",JOptionPane.INFORMATION_MESSAGE);
	}//End of showErrorMessage
	
	
	public JTabbedPane getTablePane(){
	    return super.getTablePane();
	}

	public MatrixViewTable getTable(String name){
	    int table = IndexOf(name);
	    return (MatrixViewTable)this.tables.get(table);
	}
	
	public MatrixViewTable getTable(int i){
	    return (MatrixViewTable)this.tables.get(i);
	}
	
	public Color getTableColor(String name){
	    int table = IndexOf(name);
	    return ((MatrixViewTable)this.tables.get(table)).getColor();
	}
	
	public void setTableColor(String name, Color c){
	    int table = IndexOf(name);
	    ((MatrixViewTable)this.tables.get(table)).setColor(c);
	}
	
	public JButton getCell(String name, int row, int col){
	    int table = IndexOf(name);
	    return ((MatrixViewTable)this.tables.get(table)).getCells()[row][col];
	}
	
	public String getColumnName(String name, int col){
	    int table = IndexOf(name);
	    return ((MatrixViewTable)this.tables.get(table)).getColumnNames()[col];
	}
	
	private int IndexOf(String name){
	    ListIterator tabs = tables.listIterator();
	    while(tabs.hasNext()){
	       MatrixViewTable t = (MatrixViewTable)tabs.next();
	       String n = t.getName();
	       if(n.equals(name)) return tables.indexOf(t);
	    }
	    return 0;
	}
	
	
    /**
     * @return Returns the nLines.
     */
    public int getNLines() {
        return nLines;
    }
    /**
     * @param lines The nLines to set.
     */
    public void setNLines(int lines) {
        nLines = lines;
    }
    /**
     * @return Returns the nTables.
     */
    public int getNTables() {
        return nTables;
    }
    /**
     * @param tables The nTables to set.
     */
    public void setNTables(int tables) {
        nTables = tables;
    }
    /**
     * @return Returns the tables.
     */
    public ArrayList getTables() {
        return tables;
    }
    /**
     * @param tables The tables to set.
     */
    public void setTables(ArrayList<MatrixViewTable> tables) {
        this.tables = tables;
    }
    /**
     * @return Returns the title.
     */
    public String getTitle() {
        return title;
    }
    /**
     * @param title The title to set.
     */
    public void setTitle(String title) {
        this.title = title;
    }
}// End of class

//==== End of File
